import Taro from '@tarojs/taro'
import ajax from "../../net/ajax";
import apiObject from "../../net/api";

export default {

    setToken(token) {
        Taro.setStorageSync('token', token)
    },

    getToken() {
        return Taro.getStorageSync('token')
    },
    setLevel(level) {
        Taro.setStorageSync('level', level)
    },

    getLevel() {
        return Taro.getStorageSync('level')
    },
    setUserInfo(userInfo) {
        Taro.setStorageSync('userInfo', userInfo)
    },

    getUserInfo() {
        return Taro.getStorageSync('userInfo')
    },

    setWxCode(wxCode) {
        Taro.setStorageSync('wxCode', wxCode)
    },

    getWxCode() {
        return Taro.getStorageSync('wxCode')
    },

    setPhoneNumber(phoneNumber) {
        Taro.setStorageSync('phoneNumber', phoneNumber)
    },

    getPhoneNumber() {
        return Taro.getStorageSync('phoneNumber')
    },
    isRefreshPage() {
        return Taro.getStorageSync('isRefreshPage')
    },
    setRefreshPage(isRefreshPage) {
        Taro.setStorageSync('isRefreshPage', isRefreshPage)
    },

    async getWeChatUserInfo(encryptedData, iv) {
        let res = await Taro.login();
        if (res && res.errMsg == 'login:ok') {
            let params = {
                code: res.code,
                encrypdata: encryptedData,
                ivdata: iv
            };
            let ress = await ajax.post(apiObject.getUserInfo, params);
            if (ress && ress.statusCode == 200) {
                this.setUserInfo(ress.data);
            } else {
                Taro.showToast({
                    title: '请求异常,请重试!',
                    icon: 'none',
                    duration: 2000
                })
            }
        } else {
            Taro.showToast({
                title: '获取用户信息,请重试!',
                icon: 'none',
                duration: 2000
            })
        }
    },

    async authLogin(userInfo) {
        let phone = this.getPhoneNumber();
        if (!phone) {
            phone = userInfo.openId;
        }
        const params = {
            username: phone,
            userInfo: userInfo,
        }
        await ajax.post(apiObject.userLogin, params).then(res => {
            console.log("userLogin==>", res)
            if (res.statusCode == 200) {
                let token = res.data.token;
                this.setToken(token);
            }
        })
    },

    async getUserLevel() {
        await ajax.post(apiObject.supplierLevel, {}).then(res => {
            console.log("userLogin==>", res)
            if (res.statusCode == 200) {
                let level = res.data;
                this.setLevel(level);
            }
        })
    },

    async getUserOpenId() {
        let userInfo = this.getUserInfo();
        let res = await Taro.login();
        if (res.code) {
            let ress = await ajax.post(apiObject.jscode2session, {js_code: res.code});
            if (ress.data) {
                let opneId = ress.data.openid;
                userInfo['openId'] = opneId;
                this.setUserInfo(userInfo);
                return opneId;
            }
        }
    },

    removeCache() {
        Taro.clearStorageSync();
    },

    async pullToken(){
      let userInfo = this.getUserInfo();
      let phone = this.getPhoneNumber();
      if(!phone){
        phone = userInfo.openId;
      }
      const params = {
          username: phone,
          userInfo: userInfo,
      }
      await ajax.post(apiObject.userLogin, params).then(res=>{
          console.log("userLogin==>",res)
          if(res.statusCode == 200){
            let token = res.data.token;
            this.setToken(token);
          }
      })
    },
  
    // async getUserLevel(){
    //   await ajax.post(apiObject.supplierLevel, {}).then(res=>{
    //     console.log("userLogin==>",res)
    //     if(res.statusCode == 200){
    //       let level = res.data;
    //       this.setLevel(level);
    //     }
    //   })
    // },
  
    // async getUserOpenId(){
    //   let userInfo = this.getUserInfo();
    //   let res = await Taro.login();
    //   if(res.code){
    //       let ress =  await ajax.post(apiObject.jscode2session,{js_code: res.code});
    //       if(ress.data){
    //           let opneId = ress.data.openid;
    //           userInfo['openId'] = opneId;
    //           this.setUserInfo(userInfo);
    //           return opneId;
    //       }
    //   } 
    // }
}


